package com.project.weather.service.impl;

import com.project.weather.common.AResultCode;
import com.project.weather.common.PageBean;
import com.project.weather.common.ResultMap;
import com.project.weather.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.project.weather.dao.CityMapper;
import com.project.weather.pojo.CityBean;
import com.project.weather.service.CityService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *<p>Title:CityServiceImpl.java</p>
 *<p>Description:城市表ServiceImpl</p>
 *@author xxx
 *@version 1.0
 *@Automatically generate by Coder in 2022-02-13 20:49
 */
@Service
public class CityServiceImpl implements CityService{
	private static final Logger log = LoggerFactory.getLogger(CityServiceImpl.class);
	@Autowired
	private CityMapper cityMapper;
	
	@Override
	public ResultMap<AResultCode, CityBean> insert(CityBean bean){
		ResultMap<AResultCode, CityBean> result = new ResultMap<AResultCode, CityBean>();
		if(StringUtil.isNull(bean)){
			result.setCode(AResultCode.IS_NULL);
		} else {
			if(StringUtil.isNull(bean.getId())){
				bean.setId(StringUtil.getUUID());
			}
			cityMapper.insertSelective(bean);	
			result.setCode(AResultCode.OK);
			result.setResult(bean);
		}
		return result;
	}

	
	@Override
	public ResultMap<AResultCode, CityBean> update(CityBean bean){
		ResultMap<AResultCode, CityBean> result = new ResultMap<AResultCode, CityBean>();

		if(StringUtil.isNull(bean)){
			result.setCode(AResultCode.IS_NULL);
		} else if(StringUtil.isNull(bean.getId())){
			result.setCode(AResultCode.MISS_ID);
		} else {
			cityMapper.updateSelective(bean);	
			result.setCode(AResultCode.OK);
			result.setResult(bean);
		}

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, CityBean> delete(String id){
		ResultMap<AResultCode, CityBean> result = new ResultMap<AResultCode, CityBean>();

		if(StringUtil.isNull(id)){
			result.setCode(AResultCode.MISS_ID);
		} else {
			String[] ids = id.split(",");
			cityMapper.delete(ids);
			result.setCode(AResultCode.OK);
		}

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, PageBean<CityBean>> pageList(PageBean<CityBean> pb){
		ResultMap<AResultCode, PageBean<CityBean>> result = new ResultMap<AResultCode, PageBean<CityBean>>();

		pb.setData(cityMapper.pageList(pb));
		pb.clear();

		result.setCode(AResultCode.OK);
		result.setResult(pb);

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, List<CityBean>> list(CityBean bean){
		ResultMap<AResultCode, List<CityBean>> result = new ResultMap<AResultCode, List<CityBean>>();

		List<CityBean> list = cityMapper.list(bean);
		result.setCode(AResultCode.OK);
		result.setResult(list);

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, CityBean> get(String id){
		ResultMap<AResultCode, CityBean> result = new ResultMap<AResultCode, CityBean>();
		if(StringUtil.isNull(id)){
			result.setCode(AResultCode.MISS_ID);
		} else {
			CityBean bean = cityMapper.get(id);
			if(bean != null) {
				result.setCode(AResultCode.OK);
				result.setResult(bean);
			} else {
				result.setCode(AResultCode.UNEXIST);
	 		}
		}
		return result;
	}

	@Override
	public ResultMap<AResultCode, List<Map>> listAllCity(CityBean bean) {
		ResultMap<AResultCode, List<Map>> result = new ResultMap<>();

		List<Map> resultListMap = new ArrayList<>();



		List<String> provinceList = cityMapper.getAllProvince();

		for(String p : provinceList){
			List<CityBean> pList = cityMapper.getCityByProvince(p);

			CityBean cityBean = pList.get(0);

			Map m = new HashMap();
			m.put("value",p);
			m.put("label",cityBean.getProvincezh());

			List<Map> lm = new ArrayList<>();
			for (CityBean c : pList){
				Map mm = new HashMap();
				mm.put("value",c.getId().substring(2));
				mm.put("label",c.getWea_cityzh());
				lm.add(mm);
			}
			m.put("children",lm);

			resultListMap.add(m);
		}



		result.setCode(AResultCode.OK);
		result.setResult(resultListMap);
		return result;
	}


}